
import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;
import lgxcom from '../common/common.js'

import {LGXEnums}  from '../common/sysenum.js';


import {CommonSvc}  from './commonsvc.js';

import chamferarcgraphsvc from './chamfer/chamferarcgraphsvc.js';


var chamferRadius = 10;
var rectChamferSize = 10;

var MainChamferSvc = {}

MainChamferSvc.rectChamfer = function (dataManager, mousePoint,size) {
    rectChamferSize = size;
    console.log("MainChamferSvc.rectChamfer**********************");
    let graph = this.pickSelGraph(dataManager, mousePoint);
    if (!graph || (!this.chkValidGraph(graph))) {
        return false;
    }

    //多段线、多边形都要打散为一根根离散的直线
    CommonSvc.breakGraph2LineSecs(graph, dataManager);
    var result = this.getSelTwoLine(dataManager, mousePoint);
    if (!result) {
        return false;
    }

    let posList1 = result.line1.getPosCopy().posList;
    let posList2 = result.line2.getPosCopy().posList;

    let curentPt = posList1[result.selIdx1];
    let nextPt1 = null;
    if (result.selIdx1 == 0) {
        nextPt1 = posList1[1];
    }
    else {
        nextPt1 = posList1[0];
    }

    let nextPt2 = null;
    if (result.selIdx2 == 0) {
        nextPt2 = posList2[1];
    }
    else {
        nextPt2 = posList2[0];
    }

    let vector1 = new lgxcom.LGXVector();
    vector1.setBy2Pt(curentPt, nextPt1);
    let line1Len = funcs.distancePoint(curentPt, nextPt1,1);

    let vector2 = new lgxcom.LGXVector();
    vector2.setBy2Pt(curentPt, nextPt2);
    let line2Len = funcs.distancePoint(curentPt, nextPt2,1);
    if (line1Len < rectChamferSize || line2Len < rectChamferSize){
        return false;
    }

    let x1 = curentPt.x + vector1.m * rectChamferSize;
    let y1 = curentPt.y + vector1.n * rectChamferSize;
    let line1NewPt = { x: x1, y: y1 };

    let x2 = curentPt.x + vector2.m * rectChamferSize;
    let y2 = curentPt.y + vector2.n * rectChamferSize;
    let line2NewPt = { x: x2, y: y2 };

    this.alterOldGraph(result.line1, line1NewPt,nextPt1);
    this.alterOldGraph(result.line2, line2NewPt,nextPt2);

    let info = result.line1.toInfo();
    CommonSvc.creatNewLineGraph(line1NewPt,line2NewPt,info,dataManager);
   
    return true;
}


MainChamferSvc.Roundchamfer = function (dataManager, mousePoint,radius) {
    chamferRadius = radius;
    console.log("MainChamferSvc.Roundchamfer**********************");
    let graph = this.pickSelGraph(dataManager, mousePoint);
    if (graph && graph.m_graphType == LGXEnums.LGXGraphType.ArcGraph_type){
        return chamferarcgraphsvc.ChamferArcGrahSvc.chamfer(dataManager, mousePoint,radius);
    }
    if (!graph || (!this.chkValidGraph(graph))) {
        return false;
    }

    //多段线、多边形都要打散为一根根离散的直线
    CommonSvc.breakGraph2LineSecs(graph, dataManager);
    var result = this.getSelTwoLine(dataManager, mousePoint);
    if (!result) {
        return false;
    }

    let posList1 = result.line1.getPos().posList;
    let posList2 = result.line2.getPos().posList;

    let curentPt = posList1[result.selIdx1];
    let nextPt1 = null;
    if (result.selIdx1 == 0) {
        nextPt1 = posList1[1];
    }
    else {
        nextPt1 = posList1[0];
    }

    let nextPt2 = null;
    if (result.selIdx2 == 0) {
        nextPt2 = posList2[1];
    }
    else {
        nextPt2 = posList2[0];
    }

    let vector1 = new lgxcom.LGXVector();
    vector1.setBy2Pt(curentPt, nextPt1);
    let angle1 = vector1.angleFrmXAxisPositiveDir();

    let vector2 = new lgxcom.LGXVector();
    vector2.setBy2Pt(curentPt, nextPt2);
    let angle2 = vector2.angleFrmXAxisPositiveDir();

    const lenth = 10;
    let x1 = curentPt.x + vector1.m * lenth;
    let y1 = curentPt.y + vector1.n * lenth;
    let p1 = { x: x1, y: y1 };

    let x2 = curentPt.x + vector2.m * lenth;
    let y2 = curentPt.y + vector2.n * lenth;
    let p2 = { x: x2, y: y2 };

    let mx = (x1 + x2) / 2;
    let my = (y1 + y2) / 2;
    let midpt = { x: mx, y: my };
    let midvector = new lgxcom.LGXVector();
    midvector.setBy2Pt(curentPt, midpt);

    //圆弧起始点在第二根线上
    let arcStPointInFirstLineFlag = false;
    let stVector = vector1;
    let endVector = vector2;
    if (Math.abs(angle1 - angle2) < 180) {
        //两条逆时针旋转角差值小于180°，则以小角度为起始线路
        if (angle2 < angle1) {
            stVector = vector2;
            endVector = vector1;
            arcStPointInFirstLineFlag = true;//圆弧起始点在第一根线上
        }
    }
    else {
        //两条逆时针旋转角差值大于180°，则以大角度为起始线路
        arcStPointInFirstLineFlag = true;
        stVector = vector2;
        endVector = vector1;
        if (angle2 < angle1) {
            stVector = vector1;
            endVector = vector2;
            arcStPointInFirstLineFlag = false;//圆弧起始点在第一根线上
        }
    }

    let rotAngle = stVector.angleByCrossMultiply2Another(endVector);
    let halfAngle = rotAngle / 2;
    let theta = funcs.degree2Radian(90 - halfAngle);
    let moveLenthInEdgeLine = chamferRadius * Math.tan(theta);

    let x = curentPt.x + endVector.m * moveLenthInEdgeLine;
    let y = curentPt.y + endVector.n * moveLenthInEdgeLine;
    let arcStPoint = { x, y };

    x = curentPt.x + stVector.m * moveLenthInEdgeLine;
    y = curentPt.y + stVector.n * moveLenthInEdgeLine;
    let arcEndPoint = { x, y };

    //求圆弧圆心
    let stLineInvertVct = stVector.cloneCopy();
    stLineInvertVct.rotate(90);

    let endLineInvertVct = endVector.cloneCopy();
    endLineInvertVct.rotate(90);

    let interPoint = funcs.getTwoLineInterPos(arcEndPoint, stLineInvertVct, arcStPoint, endLineInvertVct);
    if (!interPoint) {
        return false;
    }

    let arcStVector = new lgxcom.LGXVector();
    arcStVector.setBy2Pt(interPoint, arcStPoint);
    let stAngle = arcStVector.angleFrmXAxisPositiveDir();

    let arcEndVector = new lgxcom.LGXVector();
    arcEndVector.setBy2Pt(interPoint, arcEndPoint);
    let endAngle = arcEndVector.angleFrmXAxisPositiveDir();
    this.createArcGraph(interPoint, stAngle, endAngle, dataManager);
    if (arcStPointInFirstLineFlag) {
        this.alterOldGraph(result.line1, nextPt1, arcStPoint);
        this.alterOldGraph(result.line2, nextPt2, arcEndPoint);
    }
    else {
        this.alterOldGraph(result.line1, nextPt1, arcEndPoint);
        this.alterOldGraph(result.line2, nextPt2, arcStPoint);
    }
    return true;
}

MainChamferSvc.alterOldGraph = function (dstGraph, p0, p1) {
    let oldPosition = dstGraph.getPos();
    oldPosition.clear();

    let newPt = new lgxcom.LGXPoint();
    newPt.x = p0.x;
    newPt.y = p0.y;
    oldPosition.addPoint(newPt);

    newPt = new lgxcom.LGXPoint();
    newPt.x = p1.x;
    newPt.y = p1.y;
    oldPosition.addPoint(newPt);
    dstGraph.calScope(1);
}

//{line1:graph1,selIdx1:0,line2:graph2,selIdx2:1}
MainChamferSvc.getSelTwoLine = function (dataManager, mousePoint) {
    let line1 = null;
    let selIdx1 = -1;
    let line2 = null;
    let selIdx2 = -1;

    let selCount = 0;
    let scaling = dataManager.getScaling();
    let mapscale = dataManager.getMapScale();
    let tAllGraphLst = dataManager.getAllGraphs();
    for (let i = 0; i < tAllGraphLst.length; i++) {
        let dstGraph = tAllGraphLst[i];
        if (!this.chkCanChamferGraph(dstGraph)) {
            continue;
        }

        let rst = this.chkIsChamferLine(dstGraph, mousePoint, scaling, mapscale);
        if (rst && rst.flag) {
            selCount++;
            if (!line1) {
                line1 = dstGraph;
                selIdx1 = rst.idx;
            }
            else if (!line2) {
                line2 = dstGraph;
                selIdx2 = rst.idx;
            }
        }
    }
    let chamferLineRst = null;
    if (selCount == 2) {
        let p1 = line1.getPos().posList[selIdx1];
        let p2 = line2.getPos().posList[selIdx2];
        let dis = funcs.distancePoint(p1, p2, 1);
        if (dis < global.ZERO_4E) {
            chamferLineRst = { line1, selIdx1, line2, selIdx2 };
        }
    }
    return chamferLineRst;
}

MainChamferSvc.chkCanChamferGraph = function (graph) {
    if (graph.getPos().posType != LGXEnums.LGXPosMode.multiPt_Mode) {
        return false;
    }
    if (graph.getUseful() == 1
        && graph.getFinish() == 1
        && graph.getStart() == 1) {
        return true;
    }
    return false;
}

MainChamferSvc.chkIsChamferLine = function (dstGraph, mousePoint, scaling, mapscale) {
    var cmpGap = dstGraph.getSelCmpGap(scaling, mapscale);
    let oldPosList = dstGraph.getPos().posList;
    if (oldPosList.length != 2) {
        return null;
    }
    let p0 = oldPosList[0];
    if (this.chkPointBeenSel(p0, mousePoint, cmpGap, scaling)) {
        return { flag: true, idx: 0 }
    }
    let p1 = oldPosList[1];
    if (this.chkPointBeenSel(p1, mousePoint, cmpGap, scaling)) {
        return { flag: true, idx: 1 }
    }
    return null;
}

MainChamferSvc.chkPointBeenSel = function (dstPt, mousePoint, cmpGap, scaling) {
    let dis = funcs.distancePoint(mousePoint, dstPt, scaling);
    if (dis <= cmpGap && dis >= 0) {
        return true;
    }
    else {
        return false;
    }
}

MainChamferSvc.pickSelGraph = function (dataManager, mousePoint) {
    let graph = dataManager.pickGraph(mousePoint);
    return graph;
}

MainChamferSvc.createArcGraph = function (interPoint, stAngle, endAngle, dataManager) {
    var toolKind = 0x03200000;
    let pNewArcGraph = dataManager.creatNewGraph4InnerSvc(toolKind);
    if (!pNewArcGraph) {
        return null;
    }

    let position = pNewArcGraph.getPos();
    position.x = interPoint.x;
    position.y = interPoint.y;
    position.radius = chamferRadius;
    position.stAngle = stAngle;
    position.endAngle = endAngle;

    pNewArcGraph.calControlPoint();
    pNewArcGraph.calScope(1);
    return pNewArcGraph;
}


MainChamferSvc.chkValidGraph = function (graph) {
    let type = graph.m_graphType;
    if (type != LGXEnums.LGXGraphType.LineGraph_type
        && type != LGXEnums.LGXGraphType.PolygonGraph_type
        && type != LGXEnums.LGXGraphType.RectGraph_type) {
        return false;
    }
    return true;
}


export { MainChamferSvc }